import {
  Get,
  Post,
  Param,
  Delete,
  Body,
  DefaultValuePipe,
  ParseIntPipe,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { WitAdmin } from '../auth/wit.admin';
import { ApiBody, ApiOperation, ApiParam } from '@nestjs/swagger';
import { GetUser } from '../auth/get-user.decorator';
import { User } from '../user/entities/user.entity';
import { Role } from './entities/role.entity';
import { ThemeFilterDto } from '../theme/dto/themeFilterDto';
import { Pagination } from "../utils/paginate";
import { BindUserDto } from "./dto/bind-user.dto";

@WitAdmin('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post('/addRole')
  @ApiOperation({
    summary: '添加角色',
  })
  create(
    @Body() createRoleDto: CreateRoleDto,
    @GetUser() user: User,
  ): Promise<string> {
    return this.roleService.create(createRoleDto, user);
  }

  @Post('/getRoleList')
  @ApiOperation({
    summary: '获取所有角色',
  })
  findAll(
    @Body() name: ThemeFilterDto,
    @Param('pageNum', new DefaultValuePipe(0), ParseIntPipe) pageNum?: number,
    @Param('pageSize', new DefaultValuePipe(0), ParseIntPipe) pageSize?: number,
  ): Promise<Pagination<Role>> {
    const page = {
      page: pageNum,
      limit: pageSize > 100 ? 100 : pageSize,
    };
    return this.roleService.findAll(name, page);
  }
  @ApiOperation({
    summary: '通过id获取角色',
  })
  @Get('/getRole/:id')
  findOne(@Param('id') id: string): Promise<Role> {
    return this.roleService.findOne(id);
  }
  @ApiOperation({
    summary: '修改角色',
  })
  @Post('/updateRole')
  update(
    @Body() updateRoleDto: UpdateRoleDto,
    @GetUser() user: User,
  ): Promise<string> {
    return this.roleService.update(updateRoleDto, user);
  }
  @ApiOperation({
    summary: '删除角色',
  })
  @Delete('/deleteRole/:id')
  remove(@Param('id') id: string): Promise<string> {
    return this.roleService.remove(id);
  }
  @ApiOperation({
    summary: '角色绑定到用户',
  })
  @Post('/bindUser')
  bindUser(@Body() data: BindUserDto): Promise<string> {
    return this.roleService.bindUsers(data);
  }
}
